/*
 * Copyright (C) 2008 xyzse
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file XUrl.h
 * @version 0.0.1
 * @author lzz(zhengzhongliao@gmail.com), zouxin (zouxin2008@gmail.com)
 * @data 20/3/2008 0.0.1 lzz add function removeTag, delTagContent 
 * @date 18/3/2008 0.0.0 created, by zouxin
 */
#include "StrTool.h"
#include <vector>
#include <string>
#include <iostream>
/**
 * provied some useful functions to deal with the html 
 */
using namespace std;


StrTool::StrTool()
{
	
}


StrTool::~StrTool()
{
	
}

/*
 * del the content between <tag> and </tag>
 * it also can deal with the tag which has attributes
 */
void StrTool::delTagContent(string &s, string tag)
{
	string sbegin = "<" + tag;
	
	//cout << sbegin << endl;
	
	string send = "</" + tag;
	
	string::size_type first_pos = 0;
	unsigned int cnt = 0;
	

	string::size_type begin_pos = 0;
	string::size_type end_pos = 0;
	
	
	while(1)
	{
		
		if((first_pos = s.find(sbegin, first_pos)) == string::npos)
			return;
		cnt = 1;
		begin_pos = first_pos;
		if((end_pos = s.find(send, first_pos)) == string::npos)
			return;
		
		while(1)
		{
			begin_pos = begin_pos + 1 + tag.size();
			begin_pos = s.find(sbegin, begin_pos);
			if(begin_pos != string::npos && begin_pos < end_pos)
			{
				++cnt;
			}
			else 
			{
				for(unsigned int i = 1; i <= cnt - 1; ++i)
				{
					if((end_pos = s.find(send, end_pos + 2 + tag.size())) == string::npos)
						return;
				}
				
				end_pos = s.find(">", end_pos);
				s.replace(first_pos, end_pos - first_pos + 1, "");
				
				if(first_pos > 0)
					--first_pos;
				break;
			}
		}
	}
}

//replace srstr with dsstr in str
void StrTool::replaceStr(string&str,string srstr,string dsstr)
{
	if( str.size() ==0 || srstr.size() == 0 )
		return;

	string::size_type idx = 0;
	string::size_type sub_length = srstr.length();
	idx = str.find(srstr,idx);
	while( idx != string::npos ){
		str.replace(idx,sub_length,dsstr);

		if( idx+dsstr.size() > str.size() ) break;

		idx = str.find(srstr,idx+dsstr.size());
	}
}

//delete \t\r\n
void StrTool::emptyStr(string &str)
{
	const string delims(" \t\r\n");
	string::size_type idx=0, pre_idx;

	while( (idx = str.find_first_of(delims, idx)) != string::npos ){
		pre_idx = idx;
		str.replace(idx,1,1,' ');
		idx++;
		while( (idx = str.find_first_of(delims, idx)) != string::npos ) { 
			if( idx-pre_idx == 1 ){ 
				str.erase(idx, 1); 
			} else { 
				break; 
			} 
		}       

		idx--;  
	}          

}

//remove <tag> in content
void StrTool::removeTags(string &content)
{
	string::size_type begin_pos = 0;
	string::size_type end_pos = 0;
	while(1)
	{
		//cout << "good2" << endl;
		if((begin_pos = content.find("<", begin_pos)) == string::npos)
			return;
		if((end_pos = content.find(">", begin_pos)) == string::npos)
			return;
		
		content.replace(begin_pos, end_pos - begin_pos + 1, "");
		
		if(begin_pos > 0)--begin_pos;
	}

}
